package com.ymatou.iapi.m.couponservice.testcase;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.SellerCreateBatchCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
/**
*商家创建优惠券批次信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SellerCreateBatch{
	private static SellerCreateBatchBean sellercreatebatchBean;
	private static SellerCreateBatchCall sellercreatebatchCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("商家创建优惠券批次信息");
	}

	@Before
	public void caseUp() {
		sellercreatebatchBean=new SellerCreateBatchBean();
		sellercreatebatchCall=new SellerCreateBatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellercreatebatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerCreateBatch_001() {
		Logger.start(true,"商家创建优惠券批次信息-开启无门槛领取");
		try {
			sellercreatebatchBean = new CouponServiceMCallService().setDefaultSellerCreateBatchBean();
			
			sellercreatebatchCall.setData(sellercreatebatchBean);
			sellercreatebatchCall.callService();			
			
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerCreateBatch_002() {
		Logger.start(true,"商家创建优惠券批次信息-关闭无门槛领取");
		try {
			sellercreatebatchBean = new CouponServiceMCallService().setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setIsShowOnPage(false);
			
			sellercreatebatchCall.setData(sellercreatebatchBean);
			sellercreatebatchCall.callService();			
			
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerCreateBatch_003() {
		Logger.start(true,"商家创建优惠券批次信息-创建优惠券-分享券");
		try {
			sellercreatebatchBean = new CouponServiceMCallService().setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setCouponType((byte)2);
			sellercreatebatchBean.setEffectiveDays(2);
			
			sellercreatebatchCall.setData(sellercreatebatchBean);
			sellercreatebatchCall.callService();			
			
			VerifyReturnDataBase();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerCreateBatch_004() {
		Logger.start(true,"商家创建优惠券批次信息-创建优惠券，分享券允许相对有效期，对相对有效期参数进行有效性验证-无效参数");
		try {
			sellercreatebatchBean = new CouponServiceMCallService().setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setCouponType((byte)-2);
			sellercreatebatchBean.setEffectiveDays(2);
			
			sellercreatebatchCall.setData(sellercreatebatchBean);
			sellercreatebatchCall.callService();			
			
			Logger.verifyEquals("2", sellercreatebatchCall.getCode(), "验证返回Code");
			Logger.verifyIsContains("优惠券类型错误",
					sellercreatebatchCall.getMessage(), "验证返回MessageText");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SellerCreateBatch_005() {
		Logger.start(true,"商家创建优惠券批次信息-创建优惠券-分享券-先根据买手Id，最小金额，优惠面额，优惠券类型查询优惠券是否存在存在则直接返回批次编号");
		try {
			sellercreatebatchBean = new CouponServiceMCallService().setDefaultSellerCreateBatchBean();
			sellercreatebatchBean.setCouponType((byte)2);
			sellercreatebatchBean.setEffectiveDays(2);
			
			sellercreatebatchCall.setData(sellercreatebatchBean);
			sellercreatebatchCall.callService();			
			      
			String batchCode1 = sellercreatebatchCall.getBatchCode();
			
			sellercreatebatchCall.callService();			
			String batchCode2 = sellercreatebatchCall.getBatchCode();
			
			Logger.verifyEquals(batchCode2,
					batchCode1, "验证返回batchCode一致");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	public void VerifyReturnDataBase() throws ParseException {
		// 验证返回
		Logger.verifyEquals("0", sellercreatebatchCall.getCode(), "验证返回Code");
		Logger.verifyEquals("操作成功！",
				sellercreatebatchCall.getMessage(), "验证返回MessageText");

		// 验证Ymt_CouponBatch表
		String sBatchCode = sellercreatebatchCall.getBatchCode();
		List<YmtCouponBatch> list = new YmtCouponBatchWapper()
				.selectBysBatchCode(sBatchCode);

		Logger.verifyEquals(1, list.size(), "验证Ymt_CouponBatch表生成sBatchCode="
				+ sBatchCode + "的条数。");

		if (list.size() >= 1) {
			YmtCouponBatch item = list.get(0);
			Logger.verifyEquals(sellercreatebatchBean.getBatchName(),
					item.getsCouponName(), "验证数据库Ymt_CouponBatch表中sBatchCode="
							+ sBatchCode + "的记录中sCouponName");
			Logger.verifyEquals(sellercreatebatchBean.getSellerId(),
					item.getiOperatorId(), "验证数据库Ymt_CouponBatch表中sBatchCode="
							+ sBatchCode + "的记录中iOperatorId");
			Logger.verifyEquals(sellercreatebatchBean.getCouponTotalCount(),
					item.getiCouponTotalNum(),
					"验证数据库Ymt_CouponBatch表中sBatchCode=" + sBatchCode
							+ "的记录中iCouponTotalNum");
			Logger.verifyEquals(sellercreatebatchBean.getReceiveNumPerUser(),
					item.getiCouponNumPerUser(),
					"验证数据库Ymt_CouponBatch表中sBatchCode=" + sBatchCode
							+ "的记录中iCouponNumPerUser");
			Logger.verifyEquals(sellercreatebatchBean.getIsShowOnPage(),
					item.getIsShowInPage(),
					"验证数据库Ymt_CouponBatch表中sBatchCode=" + sBatchCode
							+ "的记录中IsShowOnPage");
			Logger.verifyEquals(sellercreatebatchBean.getCouponType(),
					item.getCouponType(), "验证数据库Ymt_CouponBatch表中sBatchCode="
							+ sBatchCode + "的记录中coupontype");

			// 验证Ymt_CouponSetting表
			List<YmtCouponSetting> settinglist = new YmtCouponSettingWapper()
					.selectByiCouponSettingId(item.getiCouponSettingId());

			Logger.verifyEquals(
					1,
					settinglist.size(),
					"验证Ymt_CouponSetting表生成iCouponSettingId="
							+ item.getiCouponSettingId() + "条数正确");

			if (settinglist.size() >= 1) {
				YmtCouponSetting settingItem = settinglist.get(0);
				
				SimpleDateFormat formatter = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				Logger.verifyEquals(
						formatter.format(sellercreatebatchBean.getEffectiveStartDate()),
						formatter.format(settingItem.getdValidStart()),
						"验证数据库Ymt_CouponSetting表中iCouponSettingId="
								+ item.getiCouponSettingId()
								+ "的记录中dValidStart");

				SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd");
				Date endDate = formatter.parse(formatter2
						.format(sellercreatebatchBean.getEffectiveEndDate()) + " 23:59:59");
				Logger.verifyEquals(
						formatter.format(endDate),
						formatter.format(settingItem.getdValidEnd()),
						"验证数据库Ymt_CouponSetting表中iCouponSettingId="
								+ item.getiCouponSettingId() + "的记录中dValidEnd");

				// 验证Ymt_CouponScenario表
				List<YmtCouponScenario> scenariolist = new YmtCouponScenarioWapper()
						.selectByiCouponScenarioId(settingItem.getiScenarioId());

				Logger.verifyEquals(1, scenariolist.size(),
						"验证Ymt_CouponScenario表生成iCouponScenarioId="
								+ settingItem.getiScenarioId() + "条数正确");

				// 验证Ymt_CouponValue表
				List<YmtCouponValue> valuelist = new YmtCouponValueWapper()
						.selectByiCouponSettingId(settingItem
								.getiCouponSettingId());

				Logger.verifyEquals(
						1,
						valuelist.size(),
						"验证Ymt_CouponValue表生成iCouponSettingId="
								+ settingItem.getiCouponSettingId() + "条数正确");

				if (valuelist.size() >= 1) {
					YmtCouponValue valueItem = valuelist.get(0);
					Logger.verifyEquals(sellercreatebatchBean
							.getCouponAmount(),
							new BigDecimal(valueItem.getFcouponvalue()
									.floatValue()),
							"验证数据库Ymt_CouponValue表中iCouponSettingId="
									+ settingItem.getiCouponSettingId()
									+ "的记录中的Fcouponvalue");
					Logger.verifyEquals(sellercreatebatchBean
							.getOrderMinAmount(),
							new BigDecimal(valueItem.getFminordervalue()
									.floatValue()),
							"验证数据库Ymt_CouponValue表中iCouponSettingId="
									+ settingItem.getiCouponSettingId()
									+ "的记录中的Fminordervalue");
				}

			}

		}

	}
}